﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using System.Text.RegularExpressions;

namespace Core
{
    /// <summary>
    /// 编码帮助类。
    /// </summary>
    public class MD5Helper
    {
        #region ========MD5========
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">源</param>
        /// <param name="salt">盐</param>
        /// <returns>返回加密字符串</returns>
        public static string Md5Encrypt(string str, string salt = "")
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string sb = str + salt;
            return Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToCharArray())));
        }

        /// <summary>
        /// MD5比较
        /// </summary>
        /// <param name="str">源</param>
        /// <param name="salt">盐</param>
        /// <returns>返回加密字符串</returns>
        public static bool Md5Equals(string md5Str, string str, string salt = "")
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string sb = str + salt;
            string s = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToCharArray())));
            return s.Equals(md5Str);
        }
        #endregion
    }
}
